package com.zut.oss.util;

import com.zut.oss.entity.FileInfo;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MinioUtil {
    @Resource
    private MinioClient minioClient;

    /**
     * 创建bucket桶
     */
    public void createBucket(String bucket) throws Exception{
        boolean b = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (b==false){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * 上传文件
     * @param inputStream
     * @param bucket
     * @param ObjectName
     * @throws Exception
     */
    public void uploadBucket(InputStream inputStream,String bucket,String ObjectName) throws Exception{
        boolean b = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (b==true){
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucket)
                    .object(ObjectName)
                    .stream(inputStream,-1, Integer.MAX_VALUE).build());

        }
    }


    /**
     * 列出所有桶名字
     * @throws Exception
     */
    public List<String> getAllBucket() throws Exception{

        //获得所有的桶
        List<Bucket> buckets = minioClient.listBuckets();
        //对流中的每个元素应用函数，获取每个bucket的名字
        List<String> list=buckets.stream().map(bucket ->bucket.name()).collect(Collectors.toList());
        return list;
        }



    /**
     * 列出该桶下的所有文件名字
     * @throws Exception
     */
    public List<FileInfo> getBucketAllFileName(String bucket) throws Exception{
        //返回值为文件的集合
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());
        List<FileInfo> list=new ArrayList<>();
        for (Result<Item> item: results) {
            FileInfo fileInfo=new FileInfo();
            //获得Result里的Item类型
            Item item1 = item.get();
            //获得item1的名字对应文件的名字，将其放到实体类中
            fileInfo.setFileName(item1.objectName());
            fileInfo.setDir(item1.isDir());
            fileInfo.setEtag(item1.etag());
            list.add(fileInfo);
        }
        return list;

    }

    /**
     * 下载文件
     */
    public InputStream downLoad(String bucket,String objectName)throws Exception{
        //GetObjectResponse继承InputStream
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());

    }

    /**
     * 删除桶
     */
    public void deleteBucket(String bucket)throws Exception{
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
    }

    /**
     * 删除文件
     */
    public void deleteBucketFile(String bucket,String objectName)throws Exception{
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
    }

    public String getPreviewFileUrl(String bucketName, String objectName) throws Exception{
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                .method(Method.GET)
                .bucket(bucketName).object(objectName).build();
        return minioClient.getPresignedObjectUrl(args);

    }
}

